package com.jk.controller;

import com.jk.po.ClentPO;
import com.jk.po.HotelPO;
import com.jk.po.SharingPO;
import com.jk.service.SharingService;
import com.jk.vo.CustomerVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/***
 * @ClassName: SharingController
 * @Description:TODO
 * @Auther: 王宇超
 * @Date: 2021/6/30 15:43
 * @version : V1.0
 */
@RestController
@RequestMapping("sharing")
@Api("游记操作接口")
public class SharingController {
    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private SharingService sharingService;

    /**
    * @Author  王宇超
    * @method
    * @description 查询游记信息
    * @Date 16:17 2021/6/30
    * @Param  * @param null
    * @return
    **/
    @PostMapping("querySharing")
    @ApiOperation("查询游记信息")
    public @ResponseBody List<SharingPO> querySharing(){
        List<SharingPO> list = null;
        try {
            list = sharingService.querySharing();
                logger.info("查询游记消息成功");
        }catch (Exception e) {
            logger.error("查询游记消息异常："+e.getMessage());
        }
        return list;
    }
    /**
    * @Author  郑滨
    * @method  querySharingList
    * @description 生成模板
    * @Date 21:01 2021/7/1
    * @Param  * @param
    * @return void
    **/
    @PostMapping("querySharingTemplate")
    @ApiOperation("静态模板自动生成游记信息")
    public @ResponseBody void querySharingTemplate() throws IOException {
            //获取路径
            File file = new File("");
            String path =file.getCanonicalPath();
            //字符串拼接路径
            String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"notes"+"/";

            //取所有数据
            List<CustomerVO> SharingList = sharingService.querySharingList();
            //取热度排行前二名数据
            List<CustomerVO> SharingRandom = sharingService.querySharingRandom();
            //推荐热度靠前数据 展示给用户 实时变动
            List<SharingPO> SharingRankingList = sharingService.querySharingByPopularity();

            FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
            freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
            Configuration configuration = null;
            FileWriter fileWriter = null;

            try {
                try {
                    configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                    Template template = configuration.getTemplate("notes.ftl");
                    fileWriter = new FileWriter(filePath+"notes"+".html");

                    HashMap<String, Object> map = new HashMap<String, Object>();

                    //所有数据
                    map.put("sharing",SharingList);
                    //热度排行前两名数据 加上达人展示
                    map.put("sharingRandom",SharingRandom);
                    //推荐热度靠前数据 展示给用户 实时变动
                    map.put("sharingRankingList",SharingRankingList);

                    template.process(map,fileWriter);
                    //记录日志
                    logger.info("生成游记页面模板成功");
                }catch (Exception e){
                    e.printStackTrace();
                    logger.error("生成游记页面模板异常："+e.getMessage());
                }
            }catch (Exception e){
                e.printStackTrace();
                logger.error("生成游记页面模板异常："+e.getMessage());
            }finally {
                if ( fileWriter != null){
                    try {
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


    /**
    * @Author  郑滨
    * @method  querySharingTemplateById
    * @description 描述一下方法的作用
    * @Date 21:53 2021/7/5
    * @Param  * @param id
    * @return void
    **/
    @PostMapping("querySharingTemplateById/{id}")
    @ApiOperation("游记详情页面展示")
    public @ResponseBody void querySharingTemplateById(@PathVariable("id")Integer id) throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"travelnotes"+"/";

        //根据ID取游记信息
        CustomerVO customerVO = sharingService.querySharingByID(id);

        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;

        try {
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("travelnotes.ftl");
                fileWriter = new FileWriter(filePath+"travelnotes"+id+".html");
                //实体类取值前台直接${字段名取值}
                template.process(customerVO,fileWriter);
                logger.info("根据ID生成游记详情模板成功");
            }catch (Exception e){
                e.printStackTrace();
                logger.error("根据ID生成游记详情模板异常："+e.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据ID生成游记详情模板异常："+e.getMessage());
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     * @param id
     * @author 段卓帆
     * @date 2021/7/8 14:18
     * @return 查看用户个人发表的游记
     */
    @PostMapping("querySharingListById/{id}")
    @ApiOperation(value = "查看用户个人发表的游记",produces ="id")
    public SharingPO querySharingById(@PathVariable("id")@ApiParam("用户id")Integer id){
        SharingPO sharingPO = new SharingPO();
        try {
            sharingPO =  sharingService.querySharingListById(id);
            logger.info("查看用户个人发表的游记成功");
        } catch (Exception e){
            logger.error("查看用户个人发表的游记失败:"+e.getMessage());
        }
        return sharingPO;
    }

    /**
     *
     *
     * @param id
     * @author 赵磊荣
     * @date 2021/7/8 16:31
     * @return 查看用户个人发表的游记
     */
      @GetMapping("querySharingListId/{id}")
      @ApiOperation(value = "查看用户个人发表的游记",produces ="id")
      public List<SharingPO>querySharingListId(@PathVariable("id")Integer id){
            return sharingService.querySharingListId(id);
      }

}
